#include <cblas.h> // 对于BLAS
#include <lapacke.h> // 对于LAPACK
#include <iostream>
#include <fstream>
#include <stdexcept>
#include <cmath> 
#include <vector>
#include "Sphere.hpp"
#include "Spline.hpp"

std::string directory = "src/build/";  // 目标目录

double pi = M_PI;

double F(double x)
{
    return cos(x);
}

template <typename T>

double polynomial(double x, T p) {
    return p.SplineReturn(x);  
}

int main()
{
    std::vector<double> vec={-pi, -3, -pi/2,-1.5, -0.5, 0, 0.5, 1.5, pi/2, 3, pi};
    std::vector<double> y1={F(-pi), F(-3), F(-pi/2), F(-1.5), F(-0.5), F(0), F(0.5), F(1.5), F(pi/2), F(3), F(pi)};
    double fa=0;
    double fb=0;
    std::vector<double> vec1={pi/2, pi/2+0.4, pi/2+1, pi, pi/2+pi-1, pi/2+pi-0.4, pi/2+pi, pi/2+pi+0.4, pi/2+pi+1, 2*pi, pi/2+2*pi-1, pi/2+2*pi-0.4, pi/2+2*pi};
    std::vector<double> y2={F(pi/2), F(pi/2+0.4), F(pi/2+1), F(pi), F(pi/2+pi-1), F(pi/2+pi-0.4), F(pi/2+pi), F(pi/2+pi+0.4), F(pi/2+pi+1), F(2*pi), F(pi/2+2*pi-1), F(pi/2+2*pi-0.4), F(pi/2+2*pi)};
    PPFormSpline p_p1(vec,y1,"complete",fa,fb);
    BSpline p_b1(vec,y1,3,"complete",fa,fb);
    PPFormSpline p_p2(vec1,y2,"natural");
    BSpline p_b2(vec1,y2,3,"natural");
    PPFormSpline p_p3(vec,y1,"periodic");
    BSpline p_b3(vec,y1,3,"periodic");
    for (int i = 1; i < 4; i++){
        std::ofstream file(directory + "data_test_2_p" + std::to_string(i) + ".csv");
        if (!file.is_open()) {
            std::cerr << "Failed to open file" << std::endl;
            return 1;
        }

        if(i==2){
            for (double x = pi/2; x <= pi/2+2*pi; x += 0.01) {
                double y = polynomial(x,p_p2);
                file << x << "," << y << std::endl;
            }
        }
        else{
            for (double x = -pi; x <= pi; x += 0.01) {
                double y;
                if(i==1){
                    y = polynomial(x,p_p1);
                }
                else if(i==3){
                    y = polynomial(x,p_p3);
                }
                file << x << "," << y << std::endl;
            }
        } 
        file.close();
    }

    for (int i = 1; i < 4; i++){
        std::ofstream file(directory + "data_test_2_b" + std::to_string(i) + ".csv");
        if (!file.is_open()) {
            std::cerr << "Failed to open file" << std::endl;
            return 1;
        }

        if(i==2){
            for (double x = pi/2; x <= pi/2+2*pi; x += 0.01) {
                double y = polynomial(x,p_p2);
                file << x << "," << y << std::endl;
            }
        }
        else{
            for (double x = -pi; x <= pi; x += 0.01) {
                double y;
                if(i==1){
                    y = polynomial(x,p_p1);
                }
                else if(i==3){
                    y = polynomial(x,p_p3);
                }
                file << x << "," << y << std::endl;
            }
        }
        file.close();
    }

    std::ofstream file1(directory + "data_test_2" + ".csv");
        if (!file1.is_open()) {
            std::cerr << "Failed to open file" << std::endl;
            return 1;
        }

        for (double x = -pi; x <= pi; x += 0.01) {
            double y = F(x);
            file1 << x << "," << y << std::endl;
        }
        file1.close();

    std::ofstream file2(directory + "data_test_2_n" + ".csv");
        if (!file2.is_open()) {
            std::cerr << "Failed to open file" << std::endl;
            return 1;
        }

        for (double x = pi/2; x <= pi/2+2*pi; x += 0.01) {
            double y = F(x);
            file2 << x << "," << y << std::endl;
        }
        file2.close();


    return 0; 
}